//
//  NetWorkReachability.m
//  FSEDU
//
//  Created by 张晓伟 on 2018/6/23.
//  Copyright © 2018年 青岛广电传媒电子商务有限公司. All rights reserved.
//

#import "NetWorkReachability.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <AFNetworkActivityIndicatorManager.h>

@interface NetWorkReachability()

@property(nonatomic,assign) NetWorkStatus netChangeStatus;

@end

@implementation NetWorkReachability
{
    Reachability *_reachability;
}
static NetWorkReachability *center = nil;

-(NSMutableDictionary *)info{
    if (_info == nil) {
        _info = [NSMutableDictionary dictionary];
        [_info setValue:@"网络连接异常" forKey:@"0"];
        [_info setValue:@"WIFI网络已连接" forKey:@"1"];
        [_info setValue:@"2G网络已连接" forKey:@"2"];
        [_info setValue:@"3G网络已连接" forKey:@"3"];
        [_info setValue:@"4G网络已连接" forKey:@"4"];
    }
    return _info;
}

+ (NetWorkReachability *)shareNetworkCenter{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        center = [[NetWorkReachability alloc] init];
        [center initReachability];
    });
    return center;
}

- (void)initReachability{
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    _reachability = [Reachability reachabilityForInternetConnection];
    [_reachability startNotifier];
    [self reachabilityChanged];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged) name:kReachabilityChangedNotification object:Nil];
}

- (void)reachabilityChanged{
    switch (_reachability.currentReachabilityStatus) {
        case NotReachable:
            _netChangeStatus = NetworkStatusNone;
            break;
        case ReachableViaWiFi:
            _netChangeStatus = NetworkStatusWIFI;
            break;
        case ReachableViaWWAN:{
            CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
            if ([networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
                _netChangeStatus = NetworkStatus2G;
            }else if ([networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge] ||
                      [networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA] ||
                      [networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA] ||
                      [networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA] ||
                      [networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x] ||
                      [networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0] ||
                      [networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA] ||
                      [networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]){
                _netChangeStatus = NetworkStatus3G;
            }else if ([networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE] ||
                      [networkInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]){
                _netChangeStatus = NetworkStatus4G;
            }
        }break;
            
        default:
            break;
    }
    [self postChangeNetworkNotification];
}
-(NetWorkStatus)networkState{
    return _netChangeStatus;
}
- (void)postChangeNetworkNotification{
    //这里可以发送通知  在控制器里接收
//    GDLog(@"\n------------网络状态:%@----------",self.info[[NSString stringWithFormat:@"%lu",(unsigned long)self.networkState]]);
}
@end
